$(".searchCon").on("input", function () {
    if ($(this).val().length > 0) {
        $(this).nextAll(".searchIcon").css({ display: "none" });
    } else {
        $(this).nextAll(".searchIcon").css({ display: "block" });
    }
})

$(".navBar ul").children().each(function (index) {
    $(this).attr("data-id", index);
})

$(".navBar").on("mouseenter", "li", function () {
    $(this).addClass("navAct").siblings().removeClass("navAct");
    $(this).parent().next().css({ display: "block" });
    $(this).parent().next().children().eq($(this).attr("data-id")).css({ display: "block" }).siblings().css({ display: "none" });

})
$(".navBar").on("mouseleave", "li", function () {
    $(".navBar ul li").removeClass("navAct");
    $(this).parent().next().css({ display: "none" });
})


//轮播图1
var index = 0;
var timer = null
var swiperWidth = $(".core").width();
aotoPlay();

$(".core").mouseenter(function () {
    clearInterval(timer);
})
$(".core").mouseleave(function () {
    aotoPlay();
})

function aotoPlay() {
    clearInterval(timer);
    timer = setInterval(function () {
        index++;
        move();
    }, 3000)
}

$(".banNavBar").on("click", "li", function () {
    $(this).addClass("banAct").siblings().removeClass("banAct");
    index = $(this).index();
    $(".imgBox").stop(true, true).animate({ left: -swiperWidth * index }, "fast");
})

$(".left").click(function () {
    index--;
    move();
})

$(".right").click(function () {
    index++;
    move();
})

function move() {
    if (index < 0) {
        $(".imgBox").css({ left: -swiperWidth * $(".banNavBar li").length });
        index = $(".banNavBar li").length;
        index--;
    }
    if (index > $(".banNavBar li").length) {
        $(".imgBox").css({ left: 0 });
        index = 0;
        index++;
    }

    $(".banNavBar li").eq(index >= $(".banNavBar li").length ? 0 : index).addClass("banAct").siblings().removeClass("banAct");
    $(".imgBox").stop(true, false).animate({ left: -swiperWidth * index }, function () {
        if (index >= $(".banNavBar li").length) {
            $(".imgBox").css({ left: 0 });
            index = 0;
        }
    })
}

//轮播图2
var index1 = 0;
var timer1 = null
var swiperWidth1 = $(".tmall_bd").width();
bdAotoPlay();

$(".tmall_bd").mouseenter(function () {
    clearInterval(timer1);
})
$(".tmall_bd").mouseleave(function () {
    bdAotoPlay();
})

function bdAotoPlay() {
    clearInterval(timer1);
    timer1 = setInterval(function () {
        index1++;
        bdMove();
    }, 3500)
}

$(".bd-left").click(function () {
    index1--;
    bdMove();
})

$(".bd-right").click(function () {
    index1++;
    bdMove();
})

function bdMove() {
    if (index1 < 0) {
        $(".bdBar").css({ left: -swiperWidth1 * ($(".bdTwo li").length) });
        index1 = $(".bdTwo li").length;
        index1--;
    }
    if (index1 > $(".bdTwo li").length) {
        $(".bdBar").css({ left: 0 });
        index1 = 0;
        index1++;
    }
    $(".hdCount").text(index1 >= $(".bdTwo li").length ? 1 : index1 + 1);
    $(".bdBar").stop(true, false).animate({ left: -swiperWidth1 * index1 }, function () {
        if (index1 >= $(".bdTwo li").length) {
            $(".bdBar").css({ left: 0 });
            index1 = 0;
        }
    })
}

// 淘宝公告
$(".noticeHd").on("mouseenter", "li", function () {
    $(this).children().addClass("underline").end().siblings().children().removeClass("underline");
    $(this).parent().next().children().eq($(this).index()).css({ display: "block" }).siblings().css({ display: "none" });
})

//登录后
var user = getCookie("lgc");
if (user) {
    $(".tips").html(`<a href="#">${user}</a> <span></span><div class="exit">退出</div>`).css({ fontFamily: "icomoon" });
    $(".member").children().eq(0).html(`<img src="../images/getAvatar.jpg" alt="">`);
    $(".member").children().eq(1).text(`Hi!${user}`);
    $(".memberLogin").css({ display: "none" }).next().css({ display: "flex" });
    $(".tips").mouseenter(function () {
        $(".exit").css({ display: "block" });
    })
    $(".tips").mouseleave(function () {
        $(".exit").css({ display: "none" });
    })
}
$(".exit").click(function () {
    delCookie("lgc");
    location.reload();
})

//有好货数据渲染
var good = async function () {
    var result = await searchGood();
    var { status, list } = result;
    var obj = list.values();
    var html = "";
    for (var val of obj) {
        var { goodsId, goodsName, goodsImg } = val;
        html += `<li>
            <a href="../html/indexGoods.html?goodsId=${goodsId}">
                <div class="goodImg">
                    <img src="${goodsImg}" alt="">
                </div>
                <p class="goodsName">${goodsName}</p>
                <p class="goodsDetail">
                    ${goodsName}
                </p>
            </a>
        </li>`;
    }
    $(".goodGoods").html(html);
}
good();

//猜你喜欢数据渲染
var guees = async function () {
    var result = await searchGuees();
    var { status, list } = result;
    var obj = list.values();
    var html = "";
    for (var val of obj) {
        var { goodsId, goodsName, goodsImg, goodsPrice, sales } = val;
        var price = (goodsPrice.substr(1) * 1).toFixed(2);
        html += `<li>
            <a href="../html/indexGoods.html?goodsId=${goodsId}">
                <div class="gueesImg"><img src="${goodsImg}" alt=""></div>
                <p>${goodsName}</p>
                <div class="foot">
                    <span class="moneySymbol">￥</span><span class="money">${price}</span><span
                        class="perNum">${sales}</span>
                </div>
                <div class="similar">
                    <a href="#" class="view">
                        <p>查看相似</p>
                    </a>
                </div>
            </a>
        </li>`;
    }
    $(".gueesGoods").html(html);
}
guees();




//列表条状
$(".navBar>ul>li").on("click", "a", function () {
    var goodsName = $(this).text();
    if (goodsName) {
        location.href = "../html/goodsList.html?goodsName=" + goodsName;
    }
})



